﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using SC2TV.Object;
using SC2TV.ViewModel;

namespace SC2TV
{
    public partial class StreamPage
    {
        public StreamPage()
        {
            InitializeComponent();
            var vm = DataContext as StreamViewModel;
            vm.ChatMessageLoaded += vm_ChatMessageLoaded;
        }

        void vm_ChatMessageLoaded(object sender, EventArgs e)
        {
            scrollViewer.ScrollToVerticalOffset(double.MaxValue);
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (DesignerProperties.IsInDesignTool) return;

            var vm = DataContext as StreamViewModel;
            var stream = NavigationContext.QueryString["stream"];
            var channelName = NavigationContext.QueryString["channelName"];
            await vm.Load(channelName, stream);
        }
        
        private void OpenEmotionsList(object sender, EventArgs e)
        {
            EmotionsGrid.Visibility = EmotionsGrid.Visibility.Equals(Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed;
            ContentPanel.Visibility = ContentPanel.Visibility.Equals(Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
        }

        private void EmotionSelected(object sender, SelectionChangedEventArgs e)
        {
            var emotion = (sender as LongListSelector).SelectedItem as Emotions;
            if (emotion == null) return;

            var vm = DataContext as StreamViewModel;
            vm.Message += emotion.Code + " ";
            EmotionsGrid.Visibility = Visibility.Collapsed;
            ContentPanel.Visibility = Visibility.Visible;
        }

        private void Reply(object sender, RoutedEventArgs e)
        {
            var chatMessage = (sender as MenuItem).DataContext as ChatMessage;
            if (chatMessage == null) return;

            var vm = DataContext as StreamViewModel;
            vm.Message = "[b]" + chatMessage.name + ",[/b]";
        }

        private void RichTextBoxLoaded(object sender, RoutedEventArgs e)
        {
            var RTB = sender as RichTextBox;
            var chatMessage = RTB.DataContext as ChatMessage;
            if(chatMessage == null) return;
            
            var section = (Section)XamlReader.Load(chatMessage.message);
            //нужно использовать промежуточный контейнер, 
            //т.к. напрямую если добавить Section в RichTextBox то получим эксепшн.
            var tempBlocks = new List<Block>();
            tempBlocks.AddRange(section.Blocks);

            section.Blocks.Clear();//чистим блоки в Section, т.к. мы будем использовать промежуточную колекцию.

            foreach (Block block in tempBlocks)//добавляем блоки в RTB.
            {
                RTB.Blocks.Add(block);
            }
        }

        private void OpenPrivateChat(object sender, RoutedEventArgs e)
        {
            var chatMessage = (sender as MenuItem).DataContext as ChatMessage;
            if (chatMessage == null) return;

            NavigationService.Navigate(new Uri("/PrivateMessageWriterPage.xaml?requestUrl=http://sc2tv.ru/messages/new/" + chatMessage.uid + "&userName=" + chatMessage.name + "&formID=privatemsg_new", UriKind.Relative));
        }
    }
}